#include "AVFramePP.hpp"

AVFramePP::AVFramePP()
{
    frame = av_frame_alloc();
    if (!frame)
    {
        PrintLog("Failed av_frame_alloc. INDEX_7791");
        return;
    }
}

AVFramePP::~AVFramePP()
{
    if (frame != nullptr)
    {
        av_frame_free(&frame);
    }
}

int AVFramePP::InitSetFrame(AVPixelFormat pix_fmt, int width, int height, uint8_t * buffer, int buffer_size)
{
    frame->width = width;
    frame->height = height;
    frame->format = pix_fmt;

    int ret = av_image_fill_arrays(frame->data, frame->linesize, buffer, AV_PIX_FMT_BGR24, width, height, 1);
    if (ret<0)
    {
        PrintLog("Failed av_image_fill_arrays. INDEX_7653");
        return -1;
    }

    return 0;
}

int AVFramePP::Copy(AVFrame * _frame)
{
    if (_frame == NULL || _frame->data == NULL)
    {
        return -1;
    }
    av_frame_free(&frame);
    if (_frame->width > 0 && _frame->height > 0)
    {
        if (VideoCopy(_frame))
        {
            return -1;
        }
    }
    else if (_frame->channels > 0)
    {
        if (AudioCopy(_frame))
        {
            return -1;
        }
    }
    return 0;
}

int AVFramePP::VideoCopy(AVFrame * _frame)
{
    frame = av_frame_clone(_frame);
    return 0;
}

int AVFramePP::AudioCopy(AVFrame * _frame)
{
    frame = av_frame_clone(_frame);    
    return 0;
}
